package com.ebelter.bodyfatscale.ui.activity;

import android.text.TextUtils;
import android.view.View;
import android.view.ViewTreeObserver;
import android.widget.ImageView;
import android.widget.TextView;

import com.ebelter.bodyfatscale.R;
import com.ebelter.bodyfatscale.common.Constants;
import com.ebelter.bodyfatscale.model.Birthday;
import com.ebelter.bodyfatscale.moudules.https.NetUtils;
import com.ebelter.bodyfatscale.moudules.https.interfaces.HttpResponse;
import com.ebelter.bodyfatscale.moudules.https.response.HttpsParameters;
import com.ebelter.bodyfatscale.moudules.https.response.OriginalParametersResponse;
import com.ebelter.bodyfatscale.ui.view.BirthdayView;
import com.ebelter.bodyfatscale.util.NumUtils;
import com.ebelter.bodyfatscale.util.ToastUtil;
import com.ebelter.bodyfatscale.util.UserSpUtil;
import com.ebelter.btlibrary.btble.impl.scale.model.ScaleUser;
import com.ebelter.btlibrary.util.ULog;

import java.io.Serializable;
import java.util.Calendar;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

import butterknife.BindView;
import butterknife.OnClick;

//修改身高界面
public class ModifyBirthdayActivity extends BaseActivity {


    private static final String TAG = "ModifyBirthdayActivity";
    public static final int REQUESTCODE = 333;

    public static String ORIGINAL_BIRTHDAY = "ORIGINAL_BIRTHDAY";



    @BindView(R.id.modifybirthday_back_bt)
    ImageView modifybirthday_back_bt;

    @BindView(R.id.bdv_birthday_pager)
    BirthdayView bdv_birthday_pager;

    @BindView(R.id.modifybirthday_done_tv)
    TextView modifybirthday_done_tv;

    @Override
    protected int loadLayoutById() {
        return R.layout.activity_modifybirthday;
    }

    @Override
    protected void initViews() {

    }

    Birthday originalBirthday;
    @Override
    protected void initData() {

        originalBirthday = (Birthday) getIntent().getSerializableExtra(ORIGINAL_BIRTHDAY);
        if (originalBirthday==null){
            finish();
        }

        bdv_birthday_pager.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                bdv_birthday_pager.getViewTreeObserver().removeGlobalOnLayoutListener(this);
                bdv_birthday_pager.setCurrentDate(originalBirthday.getYear(),originalBirthday.getMonth(),originalBirthday.getDay());
            }
        });

        //bdv_birthday_pager.setCurrentDate(1991,6,13);
    }


    @OnClick({ R.id.modifybirthday_back_bt,R.id.modifybirthday_done_tv})
    public void btClick(View view){
        switch (view.getId()){
            case R.id.modifybirthday_back_bt:
                finish();
                break;
            case R.id.modifybirthday_done_tv:
                modifyBirthday();
                break;


        }
    }

    private void modifyBirthday() {
        String birthday=bdv_birthday_pager.getBirthday().getFatterDay();
        ULog.i(TAG,"---"+birthday);
        if (TextUtils.equals(birthday,originalBirthday.getFatterDay())){
            //说明相同就直接提示修改成功就好了
            ToastUtil.show(R.string.Modify_Success);
            finish();
        }else {


            if (!NetUtils.available()){
                ToastUtil.show(R.string.Network_error);
                return;
            }

            Map<String,Serializable> paramMap=new HashMap<>();
            paramMap.put(HttpsParameters.USER_ID, UserSpUtil.readLong(Constants.IUser.USER_ID));
            paramMap.put(HttpsParameters.BIRTHDAY, birthday);

            NetUtils.getInstance().updateInfo(this, paramMap, new HttpResponse<OriginalParametersResponse>() {
                @Override
                public void result(boolean isRequestSuccess, String requestDes, OriginalParametersResponse originalParametersResponse) {
                    if (isRequestSuccess){

                        if (originalParametersResponse.resultCode==0){
                            ToastUtil.show(R.string.Modify_Success);
                            updateLocalData((HashMap<String, Serializable>) originalParametersResponse.originalParameters);
                            finish();
                        }else {
                            ToastUtil.show(R.string.Modify_Failed);
                        }

                    }else {
                        ToastUtil.show(R.string.Network_connection_error);
                    }



                }
            });


        }
    }

//    private int getAgeByBirthday(int year){
//        Calendar calendar = Calendar.getInstance();
//        return calendar.get(Calendar.YEAR)-year;
//    }
    //更新本地数据
    private void updateLocalData(HashMap<String, Serializable> originalParameters) {
        Set<String> sets = originalParameters.keySet();
        for (String k:sets){
            Serializable serializable = originalParameters.get(k);
            if (TextUtils.equals(HttpsParameters.BIRTHDAY,k)){
                //说明更改了身高
                UserSpUtil.writeString(Constants.IUser.BIRTHDAY, (String) serializable);
                int year=getBirthdayIntValue((String) serializable,0,1990);
                UserSpUtil.writeInt(Constants.IUser.YEAR,year);

                originalBirthday.setYear(year);
                int mon=getBirthdayIntValue((String) serializable,1,6);
                UserSpUtil.writeInt(Constants.IUser.MONTHS,mon);

                originalBirthday.setMonth(mon);
                int day=getBirthdayIntValue((String) serializable,2,1);
                UserSpUtil.writeInt(Constants.IUser.DAY,day);
                
                originalBirthday.setDay(day);
                UserSpUtil.writeInt(Constants.IUser.AGE,getAgeByBirthday(year));
                if (!Constants.isGuestModel){
                    ScaleUser.getUser().setAge(getAgeByBirthday(year));
                }

            }
        }
    }


    private int getBirthdayIntValue(String string,int index,int defaultValue){
        if (TextUtils.isEmpty(string)){
            return defaultValue;
        }
        String[] args = string.split("-");
        if (args.length!=3){
            return defaultValue;
        }
        return NumUtils.string2Int(args[index],defaultValue);
    }
    private int getAgeByBirthday(int year){
        Calendar calendar = Calendar.getInstance();
        return calendar.get(Calendar.YEAR)-year;
    }
}
